﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using Moq;
using System.Diagnostics;

namespace Opo.Perspective.Core
{
    [TestFixture(Description = "Tests for Opo.Perspective.Core.PerspectiveQuery")]
    public class PerspectiveQueryTests
    {
        private string _queryUnordered;
        private string _queryOrdered;
        private string[] _tags;
        private string[] _searchTerms;

        [TestFixtureSetUp]
        public void Setup()
        {
            _tags = new string[]
            {
                "Tag1", "Tag2", "Tag3"
            };
            _searchTerms = new string[]
            {
                "SearchTerm1", "SearchTerm2", "SearchTerm3"
            };
            _queryOrdered = String.Format("tag:{0} tag:{1} tag:{2} {3} {4} {5}", _tags[0], _tags[1], _tags[2], _searchTerms[0], _searchTerms[1], _searchTerms[2]);
            _queryUnordered = String.Format("tag:{0} tag:{1} {3} {4} tag:{2} {5}", _tags[0], _tags[1], _tags[2], _searchTerms[0], _searchTerms[1], _searchTerms[2]);
        }

        [TestFixtureTearDown]
        public void Teardown()
        {

        }

        [Test]
        public void CanCreatePerspectiveQuery()
        {
            IPerspectiveQuery perspectiveQuery = new PerspectiveQuery();
            Assert.That(perspectiveQuery.Tags, Is.Not.Null);
            Assert.That(perspectiveQuery.SearchTerms, Is.Not.Null);
        }

        [Test]
        public void CanAddTagsToPerspectiveQuery()
        {
            IPerspectiveQuery perspectiveQuery = new PerspectiveQuery();
            TagCollection tags = new TagCollection();
            perspectiveQuery.Tags.Add(_tags[0]);
            perspectiveQuery.Tags.Add(new Tag(_tags[1]));

            Assert.That(perspectiveQuery.Tags.Count, Is.EqualTo(2));
        }
        
        [Test]
        public void CanParsePerspectiveQuery()
        {
            IPerspectiveQuery perspectiveQuery = PerspectiveQuery.Parse(_queryUnordered);
            Assert.That(perspectiveQuery.Tags.Count, Is.EqualTo(3));
            Assert.That(perspectiveQuery.SearchTerms.Count, Is.EqualTo(3));
        }

        [Test]
        public void CanConvertPerspectiveQueryToString()
        {
            IPerspectiveQuery perspectiveQuery = new PerspectiveQuery();
            foreach (string tag in _tags)
            {
                perspectiveQuery.Tags.Add(tag);
            }
            foreach (string searchTerm in _searchTerms)
            {
                perspectiveQuery.SearchTerms.Add(searchTerm);
            }

            Assert.That(perspectiveQuery.ToString(), Is.EqualTo(_queryOrdered));
        }
    }
}
